<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background-color: #f2f2f2;
        }

        canvas {
            background-color: #fff;
        }
    </style>
</head>
<body>
    <canvas id="myCanvas" width="400" height="300">
        您的浏览器不支持 Canvas
    </canvas>
    <form action="#" onsubmit="return false" name="myForm">
        <div>
            <label for="align">textAlign</label>
            <input type="radio" name="align" value="left"> left
            <input type="radio" name="align" value="right"> right
            <input type="radio" name="align" value="center"> center
            <input type="radio" name="align" value="start"> start
            <input type="radio" name="align" value="end"> end

            <button onclick="changeTextAttr()">修改Align效果</button>
        </div>
    </form>
    <script>
         /** @type {HTMLCanvasElement} */
        var canvas = document.getElementById('myCanvas');
        var ctx = canvas.getContext('2d');

        ctx.font = '30px Arial';
        
        function changeTextAttr() {
            let form = document.forms.myForm;
            let elements = form.elements;
            let align = elements.align.value;
            if (align) {
                ctx.textAlign = align;
            }
            ctx.beginPath();
            
            ctx.fillText('Hello world', 50, 50);
            ctx.strokeText('Hello world', 50, 100);
            ctx.closePath();
        }

        changeTextAttr();
    </script>
</body>
</html>